<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ConfigsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data =  DB::table("config_groups")->orderBy("weight","desc")->get();
        $data = _toArray($data);
        foreach($data as $k=>$v){
            $re = DB::table("configs")->where("group_id",$v['id'])->orderBy("id","asc")->get();
            $data[$k]['data'] = _toArray($re);
        }
        return view("admin/config/index")->with("data",$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = DB::table("config_groups")->orderBy("weight","desc")->get();
        return view("admin/config/create")->with("data",$data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 表单验证
        $this->validate($request, [
            'name' => 'required',
            'type' => 'required',
            'group_id' => 'required',
        ],[
            'name.required' => '配置名称不能为空',
            'type.required' => '配置类型不能为空',
            'group_id.required' => '配置分组不能为空',
        ]);

        $data = $request->except('_token');
        $data['value'] = empty($data['value']) ? "" : $data['value'];
        if (DB::table('configs')->insert($data)) {
            return redirect('/admin/configs/index');
        } else {
            return redirect('/admin/configs/create');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $configGroupData = DB::table("config_groups")->orderBy("weight","desc")->get();
        $olddata = DB::table('configs')->find($id);
        $assign = compact("configGroupData", "olddata");
        return view('admin/config/edit',$assign);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // 表单验证
        $rule = new \Illuminate\Validation\Rule;
        $this->validate($request, [
            'name' => 'required|regex:/\w{2,20}/',
            'name' => $rule->unique('configs')->ignore($id),
            'type' => 'required',
            'group_id' => 'required',
        ],[
            'name.required' => '参数名称不能为空',
            'name.regex' => '参数名称长度为2-20位',
            'name.unique' => '参数名称不能重复',
            'type.required' => '配置类型不能为空',
            'group_id.required' => '配置分组不能为空',
        ]);

        $data = $request->except('_token');
        $data['value'] = empty($data['value']) ? "" : $data['value'];
        if (DB::table('configs')->where('id', '=', $id)->update($data)) {
            return redirect('/admin/configs/index');
        } else {
            return redirect('/admin/configs/edit/'.$data['id']);
        }
    }

    public function update_all(Request $request)
    {
        $data = $request->post();
        foreach($data as $k=>$v){
            DB::table("configs")->where("name",$k)->update(["value"=>"$v"]);
        }
        return redirect('/admin/configs/index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DB::table('configs')->where('id', '=', $id)->delete();
        return redirect('/admin/configs/index');
    }
}
